const app = getApp();
Page({
  data: { 
    repairList: [],
    category:''
  },
 
  // ListTouch触摸开始
  ListTouchStart(e) {
    this.setData({
      ListTouchStart: e.touches[0].pageX
    })
  },

  // ListTouch计算方向
  ListTouchMove(e) {
    this.setData({
      ListTouchDirection: e.touches[0].pageX - this.data.ListTouchStart > 0 ? 'right' : 'left'
    })
  },

  // ListTouch计算滚动
  ListTouchEnd(e) {
    if (this.data.ListTouchDirection =='left'){
      this.setData({
        modalName: e.currentTarget.dataset.target
      })
    } else {
      this.setData({
        modalName: null
      })
    }
    this.setData({
      ListTouchDirection: null
    })
  },

  toDetails(e){
    wx.navigateTo({
      url: "/pages/labs/repairDetails?index="+ e.currentTarget.dataset.index,
    })
  },

  // 获取数据列表
  getList(){
    wx.cloud.callFunction({
      name:'getRepairList',
      data:{
        category : this.data.category
      }
    }).then(
      res=>{
        // console.log(res)
        this.setData({
          repairList: res.result.data
        })
      }
    )
  },

  onLoad(options){
    this.setData({
      category: options.category
    })
    this.getList()
  },

  // 处理就是删除 2个表中的对应记录
  handleRemove(e){
    let id = e.currentTarget.dataset.index  // 被处理的ID
    console.log(id)
    // repairList
    wx.cloud.callFunction({
      name:'removeRepair',
      data:{
        _id: id
      }
    })
    // repairDetails
    wx.cloud.callFunction({
      name:'removeRepairDetails',
      data:{
        _id: id
      }
    }).then(()=>{
      wx.showToast({
        title: '成功处理',
      })
      this.getList()
    })
  }
})